package 计算股票;

import java.util.ArrayList;
import java.util.List;

public class Test {
	/**
	 * 有n只股票 每只股票每天收益率为R 每只股票的市值为m 每天股票组合的加权收益为m/M*R
	 * 计算股票组合以t=0为节点到t=20二十天的平均收益()
	 */

	static List<List<Model>> list = new ArrayList<List<Model>>();

	public static void main(String[] args) {
		init();
		int l = list.size();
		double res[] = new double[l];
		for (int i = 0; i < l; i++) {// 第i天
			int size = list.get(i).size();
			double mi[] = new double[size];
			double ri[] = new double[size];
			for (int j = 0; j < size; j++) {
				mi[j] = list.get(i).get(j).m;
				ri[j] = list.get(i).get(j).r;
			}
			double resf = 0;
			int lf = mi.length;
			double M = 0;
			for (int k = 0; k < lf; k++) {
				M += mi[k];
			}
			for (int k = 0; k < lf; k++) {
				resf += (mi[k] / M) * ri[k];
			}
			res[i] = resf;
		}
		for (int i = 0; i < res.length; i++) {
			System.out.println(res[i]);
		}
	}

	@SuppressWarnings("unused")
	private static double calculate(double[] mi, double[] ri) {
		double resf = 0;
		int lf = mi.length;
		double M = 0;
		for (int k = 0; k < lf; k++) {
			M += mi[k];
		}
		for (int k = 0; k < lf; k++) {
			resf += (mi[k] / M) * ri[k];
		}

		return resf;
	}

	private static void init() {

		List<Model> zero = new ArrayList<Model>();
		zero.add(new Model("A", 1000, 0.4));
		zero.add(new Model("B", 1000, 0.2));

		List<Model> one = new ArrayList<Model>();
		one.add(new Model("A", 1400, 0.2));
		one.add(new Model("B", 1200, 0.2));

		list.add(zero);
		list.add(one);

	}

}

class Model {
	String name;
	double m;
	double r;

	public Model(String name, double m, double r) {
		super();
		this.name = name;
		this.m = m;
		this.r = r;
	}
}